package com.example.opengldemo.simple_13_02;

import java.util.ArrayList;

public class VectorUtil {

	public static float[] normalizeVector(float [] vec){
		float mod=module(vec);
		return new float[]{vec[0]/mod, vec[1]/mod, vec[2]/mod};
	}

	public static float module(float [] vec){
		return (float) Math.sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]);
	}

	public static float[] crossTwoVectors(float[] a, float[] b)
	{
		float x=a[1]*b[2]-a[2]*b[1];
		float y=a[2]*b[0]-a[0]*b[2];
		float z=a[0]*b[1]-a[1]*b[0];
		return new float[]{x, y, z};
	}

	public static float dotTwoVectors(float[] a, float[] b)
	{
		return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
	}


	public static float[] calTextures(
			ArrayList<Float> alST,
			ArrayList<Integer> alTexIndex
			)
	{
		float[] textures=new float[alTexIndex.size()*2];

		int stCount=0;
		for(int i:alTexIndex){
			textures[stCount++]=alST.get(2*i);
			textures[stCount++]=alST.get(2*i+1);
		}
		return textures;
	}
	public static float[] calVertices(
			ArrayList<Float> alv,
			ArrayList<Integer> alFaceIndex
			)
	{
		float[] vertices=new float[alFaceIndex.size()*3];

		int vCount=0;
		for(int i:alFaceIndex){
			vertices[vCount++]=alv.get(3*i);
			vertices[vCount++]=alv.get(3*i+1);
			vertices[vCount++]=alv.get(3*i+2);
		}
		return vertices;
	}

	public static float[] devideBall(
			float r,
			float[] start,
			float[] end,
			int n,
			int i
			)
	{
		float[] s=VectorUtil.normalizeVector(start);
		float[] e=VectorUtil.normalizeVector(end);
		if(n==0){
			return new float[]{s[0]*r, s[1]*r, s[2]*r};
		}

		double angrad=Math.acos(VectorUtil.dotTwoVectors(s, e));
		double angrad1=angrad*i/n;
		double angrad2=angrad-angrad1;

		float[] normal=VectorUtil.crossTwoVectors(s, e);

		double matrix[][]={
				{s[0],s[1],s[2],Math.cos(angrad1)},
				{e[0],e[1],e[2],Math.cos(angrad2)},
				{normal[0],normal[1],normal[2],0}
		};
		double result[]=MyMathUtil.doolittle(matrix);

		float x=(float) result[0];
		float y=(float) result[1];
		float z=(float) result[2];

		return new float[]{x*r, y*r, z*r};
	}

	public static float[] devideLine(
			float[] start,
			float[] end,
			int n,
			int i
			)
	{
		if(n==0){
			return start;
		}
		float[] ab=new float[]{end[0]-start[0], end[1]-start[1], end[2]-start[2]};
		float vecRatio=i/(float)n;
		float[] ac=new float[]{ab[0]*vecRatio, ab[1]*vecRatio, ab[2]*vecRatio};
		float x=start[0]+ac[0];
		float y=start[1]+ac[1];
		float z=start[2]+ac[2];
		return new float[]{x, y, z};
	}
}
